﻿
Partial Public Class distributor
    Protected redundance_count As _do(Of UInt32) = Nothing

    Private Shared Function step_index_to_index(ByVal d As _do(Of UInt32, Boolean)) _
                                               As _do(Of UInt32, UInt32, Boolean)
        assert(Not d Is Nothing)
        Return Function(ByRef [step] As UInt32, ByRef queue_index As UInt32) As Boolean
                   Return d(queue_index)
               End Function
    End Function

    Protected Function foreach_redundance_queue(Of T)(ByVal j As T,
                                                      ByVal d As _do(Of UInt32, UInt32, Boolean),
                                                      ByVal redundance_count As UInt32) As Boolean
        assert(Not d Is Nothing)
        assert(redundance_count > 0)
        Dim index As UInt32 = 0
        index = hashcode(j)
        For i As UInt32 = 0 To redundance_count - 1
            If d(unref(i), unref(index)) Then
                index = nextQueueIndex(index)
            Else
                Return False
            End If
        Next

        Return True
    End Function

    Protected Function foreach_redundance_queue(Of T)(ByVal j As T,
                                                      ByVal d As _do(Of UInt32, UInt32, Boolean)) As Boolean
        assert(Not redundance_count Is Nothing)
        Return foreach_redundance_queue(j, d, redundance_count())
    End Function

    Protected Function foreach_redundance_queue(Of T)(ByVal j As T,
                                                      ByVal d As _do(Of UInt32, Boolean),
                                                      ByVal redundance_count As UInt32) As Boolean
        Return foreach_redundance_queue(j, step_index_to_index(d), redundance_count)
    End Function

    Protected Function foreach_redundance_queue(Of T)(ByVal j As T,
                                                      ByVal d As _do(Of UInt32, Boolean)) As Boolean
        assert(Not redundance_count Is Nothing)
        Return foreach_redundance_queue(j, step_index_to_index(d), redundance_count())
    End Function
End Class
